package com.hdf.etdemo.http;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;

import org.apache.http.Header;
import org.json.JSONObject;

import com.hdf.easytools.base.EasyToolsActivity;
import com.hdf.easytools.http.HttpUtil;
import com.hdf.etdemo.EasyToolsApplication;
import com.hdf.etdemo.R;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.BinaryHttpResponseHandler;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.nostra13.universalimageloader.core.ImageLoader;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.BitmapFactory;
import android.media.Image;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

public class HttpActivity extends EasyToolsActivity {
	private TextView textView; // 顶部textview
	private ProgressDialog pDialog;
	private TextView textView2; // 下面textview，显示获取的所有数据
	private ImageView image;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_http);
		/** 设置标题显示 */
		this.TitleVisib(0, 1, 1, 0, 0);
		/** 绑定标题点击事件 */
		this.TitleClick(0, 1, 0, 0, 0);
		/** 设置标题样式ʽ */
		this.TitleStyle(null, R.drawable.et_back, "AsyncHttpClient", null, 0);
		textView = (TextView) findViewById(R.id.text);
		textView2 = (TextView) findViewById(R.id.text2);
		image=(ImageView) findViewById(R.id.image);
	}

	public void method1(View view) {
		pDialog = ProgressDialog.show(this, "请稍等", "数据加载中");
		String urlString = "http://task.cardwise.cn/api.php?action=getUserInfo&openid=ovsO_s-GMsEgWLXWJK3K3SCuy8MU";
		HttpUtil.post(urlString, new JsonHttpResponseHandler() {
			public void onFinish() { // 完成后调用，失败，成功，都要掉
				pDialog.dismiss();
			}

			@Override
			public void onSuccess(int statusCode, Header[] headers, JSONObject jsonobject) {
				// TODO Auto-generated method stub
				super.onSuccess(statusCode, headers, jsonobject);
				textView.setText("获取json数据成功，看下面");
				textView2.setText(jsonobject.toString());
				image.setVisibility(View.GONE);
				textView2.setVisibility(View.VISIBLE);
				
			}

			@Override
			public void onFailure(int statusCode, Header[] headers, Throwable throwable, JSONObject errorResponse) {
				// TODO Auto-generated method stub
				super.onFailure(statusCode, headers, throwable, errorResponse);
				textView2.setText(errorResponse.toString());
			}
		});
	}
	/**
	 * 下载文件
	 * @param view
	 */
	public void method3(View view) {
		pDialog = ProgressDialog.show(this, "请稍等", "数据加载中");
		//String urlString ="http://118.192.152.195:8080/112233.png";
		String urlString = "http://chuantu.biz/t2/24/1453120455x-1376440134.jpg";
		HttpUtil.post(urlString, new BinaryHttpResponseHandler() {
			public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
				File file = new File(EasyToolsApplication.BASE_IMAGE_CACHE);
				if (!file.exists()) {
					file.mkdirs();
				}
				file = new File(file, "liuyifei2.jpg");
				try {
					FileOutputStream oStream = new FileOutputStream(file);
					oStream.write(arg2);
					oStream.flush();
					oStream.close();
					fileIsExists(EasyToolsApplication.BASE_IMAGE_CACHE + "liuyifei2.jpg");
				} catch (Exception e) {
					e.printStackTrace();
					Log.i("file", e.toString());
				}
			};

			public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3) {
				Log.e("BinaryHttpResponseHandler", arg3.toString());
				textView.setText("下载出现异常"+arg3.toString());
			};

			public void onFinish() { // 完成后调用，失败，成功，都要掉
				pDialog.dismiss();
			}
		});

	}

	public void fileIsExists(String path) {
		try {
			File f = new File(path);
			if (f.exists()) {
				textView.setText("女神保存在" +path);
				image.setVisibility(View.VISIBLE);
				textView2.setVisibility(View.GONE);
				//ImageLoader.getInstance().displayImage(path, image);
				 FileInputStream fis = new FileInputStream(path);
				 image.setImageBitmap(BitmapFactory.decodeStream(fis));
			}else
				textView2.setText("文件不存在");

		} catch (Exception e) {
			// TODO: handle exception
		
			
		}
		
	}

	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.left_img:
				finish();
			break;

		default:
			break;
		}
	}

}
